package com.xiaobear.service;

import com.xiaobear.domain.GenConfig;
import com.xiaobear.mapper.GenConfigMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Version 1.0
 * @ClassName: GenConfigServiceImpl
 * @Date 2024/3/5 16:26
 * @Description: TODO
 * @Author coffee
 */
@Service
public class GenConfigServiceImpl implements IGenConfigService {

    @Resource
    private GenConfigMapper genConfigMapper;

    @Override
    public List<GenConfig> list(GenConfig genConfig) {
        return genConfigMapper.list(genConfig);
    }

    /**
     * 获取默认配置
     *
     * @return
     */
    @Override
    public GenConfig getDefaultConfig() {
        GenConfig defaultConfig = genConfigMapper.getDefaultConfig();
        if (defaultConfig == null) {
            throw new RuntimeException("请先进行项目配置 !");
        }
        return defaultConfig;
    }

    @Override
    public boolean changeState(GenConfig genConfig) {
        String configState = genConfig.getConfigState();
        Long configId = genConfig.getConfigId();

        if( "0".equals(configState)){ // 想要把配置设置为启用

            // 当想要修改为启动时，先把所有配置修改为停用
            genConfigMapper.changeStateToDeactivate(null);

            // 再把想要启动的配置，设置为启用
             genConfigMapper.changeStateToEnable(configId);

        }else{ // 想要把配置设置为停用

            genConfigMapper.changeStateToDeactivate(configId);

        }

        return true;
    }

    @Override
    public boolean addConfig(GenConfig genConfig) {

        return genConfigMapper.addConfig(genConfig);
    }

    @Override
    public boolean deleteConfig(Long[] ids) {
        return genConfigMapper.deleteConfig(ids);
    }

    @Override
    public boolean updateConfig(GenConfig genConfig) {
        return genConfigMapper.updateConfig(genConfig);
    }
}
